let app = getApp()
let yuming = app.globalData.yuming
// ajax
const myrequest = (url, data = {}, method) => {
  var token = wx.getStorageSync("token");
  var header = {
    "content-type": "application/x-www-form-urlencoded",
    token: token ? token : "",
    source: 'EabEnd'
  }
  return new Promise((resolve, reject) => {
    // wx.showLoading({
    //   title: '加载中',
    // })
    var that = this
    wx.request({
      url: url,
      data: data,
      header: header,
      method: method,
      success: (res) => {
        console.log(54545455)
        wx.hideLoading({
          success: (res) => {},
        })
        var pages = getCurrentPages()
        var currentPage = pages[pages.length - 1]
        var url = currentPage.route //当前页面url
        var options = currentPage.options
        // console.log(url)
        // console.log(options)
        if (res.data.code == 202) {
          wx.showModal({
            title: '需要授权哦~',
            cancelText: '取消',
            confirmText: '确定',
            success: function (res) {
              if (res.confirm) {
                console.log("确定")
                wx.navigateTo({
                  url: '/page_zhu/shou/shou?sta=66&url=' + url,
                })
              } else {
                wx.navigateBack({
                  delta: 0,
                })
                console.log("取消")
              }
            }
          })
        }
        // if (res.data.code == 209) {
        //   wx.showModal({
        //     title: '该账户已被删除！',
        //     cancelText: '取消',
        //     confirmText: '注册',
        //     success: function (res) {
        //       if (res.confirm) {
        //         console.log("确定")
        //         wx.clearStorage({
        //           success: (res) => {},
        //         })
        //         wx.reLaunch({
        //           url: '/page_zhu/login/login',
        //         })
        //       } else {
        //         wx.clearStorage({
        //           success: (res) => {},
        //         })
        //         wx.reLaunch({
        //           url: '/page_zhu/login/login',
        //         })
        //       }
        //     }
        //   })
        // }
        wx.hideLoading();
        resolve(res)
      },
      fail: (error) => {
        reject(error);
      }
    })
  })
}

function myRequest(url, data = {}, method) {
  return myrequest(url, data, method);
}
// 图片
const myChooseImage = (count) => {
  return new Promise((resolve, reject) => {
    wx.chooseImage({
      count: count,
      sizeType: ['original'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        let types = res.tempFiles[0].path.substring(res.tempFiles[0].path.length - 3);
        if (types == 'jpg' || types == 'png') {
          resolve(res)
        } else {
          wx.showToast({
            title: '只支持jpeg/jpg/png格式', //标题
            icon: 'none' //图标 none不使用图标，详情看官方文档
          })
          return;
        }
      },
      fail: (error) => {
        reject(error);
      }
    })
  })
}

// 图片
const jiankongimage = (count) => {
  return new Promise((resolve, reject) => {
    wx.chooseImage({
      count: count,
      sizeType: ['original'],
      sourceType: ['camera'],
      success: (res) => {
        let types = res.tempFiles[0].path.substring(res.tempFiles[0].path.length - 3);
        if (types == 'jpg' || types == 'png') {
          resolve(res)
        } else {
          wx.showToast({
            title: '只支持jpeg/jpg/png格式', //标题
            icon: 'none' //图标 none不使用图标，详情看官方文档
          })
          return;
        }
      },
      fail: (error) => {
        reject(error);
      }
    })
  })
}

const myUploadImg = (imgList, file,add_yasuo) => {
  return new Promise((resolve, reject) => {
    wx.showLoading({
      title: '正在上传图片',
    })
    // console.log(add_yasuo,add_yasuo==1? add_yasuo:'' )
    wx.uploadFile({
      url: yuming + 'Besall/Upload',
      filePath: imgList,
      name: file,
      formData: {
        // 0不加 1加水印
        'add_shuiyin': '',
        'add_yasuo': add_yasuo==1? add_yasuo:'' 
      },
      success: (res) => {
        wx.hideLoading()
        resolve(res)
      },
      fail: (error) => {
        reject(error);
      }
    })
  })
}

// 多图上传有水印
const duoupimg = (tempFilePaths,add_yasuo) => {
  return new Promise((presolve, preject) => {
    // wx.showLoading({
    //   title: '上传图片中...',
    //   mask: true
    // });
    console.log(add_yasuo)
    wx.uploadFile({
      url: yuming + 'Besall/Upload',
      filePath: tempFilePaths,
      name: 'file',
      formData: {
        'add_shuiyin': '',
        'add_yasuo': add_yasuo==1? add_yasuo:'' 
      },
      success: (res) => {
        // wx.hideLoading()
        presolve(res)
      },
      fail: (error) => {
        preject(error);
      }
    })
  })
}

// 多图上传无水印
const wuduoupimg = (tempFilePaths) => {
  return new Promise((presolve, preject) => {
    wx.showLoading({
      title: '上传图片中...',
      mask: true
    });
    wx.uploadFile({
      url: yuming + 'Besall/Upload',
      filePath: tempFilePaths,
      name: 'file',
      success: (res) => {
        wx.hideLoading()
        presolve(res)
      },
      fail: (error) => {
        preject(error);
      }
    })
  })
}

module.exports = {
  myRequest,
  myChooseImage,
  jiankongimage,
  myUploadImg,
  duoupimg,
  wuduoupimg
}